#include "volumetool.h"
#include "ui_volumetool.h"
#include<QGraphicsDropShadowEffect>
#include<QPainter>

VolumeTool::VolumeTool(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::VolumeTool),
    isMuted(false),
    volumeRatio(20)
{
    ui->setupUi(this);
    setWindowFlags(Qt::Popup|Qt::FramelessWindowHint|Qt::NoDropShadowWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);

    QGraphicsDropShadowEffect *shadowEffect=new QGraphicsDropShadowEffect(this);
    shadowEffect->setColor("#646464");
    shadowEffect->setBlurRadius(10);
    shadowEffect->setOffset(0,0);
    setGraphicsEffect(shadowEffect);


    QRect rect=ui->outLine->geometry();
    ui->outLine->setGeometry(rect.x(),rect.height()-36+25,rect.width(),36);

    ui->silderBtn->move(ui->silderBtn->x(),ui->outLine->y()-ui->silderBtn->height()/2);
    ui->VolumeBox->installEventFilter(this);

}

VolumeTool::~VolumeTool()
{
    delete ui;
}

void VolumeTool::paintEvent(QPaintEvent *event)
{
    //画音量键下方的三角形
    QPainter painter(this);
    painter.setPen(Qt::NoPen);
    painter.setBrush(QBrush(Qt::white));

    QPolygon polygon;
    QPoint a(10,300);
    QPoint b(10+80,300);
    QPoint c(10+40,300+20);

    polygon.append(a);
    polygon.append(b);
    polygon.append(c);

    painter.drawPolygon(polygon);

}

bool VolumeTool::eventFilter(QObject *watched, QEvent *event)
{
    if(ui->VolumeBox==watched){
        if(event->type()==QEvent::MouseButtonPress){
            calVolume();
        }
        else if(event->type()==QEvent::MouseButtonRelease){

            emit setMusicVolime(volumeRatio);
        }
        else if(event->type()==QEvent::MouseMove){
            calVolume();
            emit setMusicVolime(volumeRatio);
        }
        return true;
    }
    return QObject::eventFilter(watched,event);
}

void VolumeTool::calVolume()
{
   int height=ui->VolumeBox->mapFromGlobal(QCursor().pos()).y();
   height=height<25?25:height;
   height=height>205?205:height;

   ui->outLine->setGeometry(ui->outLine->x(),height,ui->outLine->width(),205-height);
   ui->silderBtn->move(ui->silderBtn->x(),ui->outLine->y()-ui->silderBtn->height()/2);

   volumeRatio=(int)(ui->outLine->height()/(float)180*100);

   ui->volimeRatio->setText(QString::number(volumeRatio)+"%");
}

void VolumeTool::on_slienceBtn_clicked()
{
    isMuted=!isMuted;
    if(isMuted){
        ui->slienceBtn->setIcon(QIcon(":/images/silent.png"));
    }
    else{
        ui->slienceBtn->setIcon(QIcon(":/images/volumn.png"));
    }

    emit setMusicMuted(isMuted);
}
